/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;
import org.xml.sax.SAXException;
import org.w3c.dom.traversal.*;

/**
 *
 * @author aptech
 */
public class StudentIterator {
    void iterate(){
        try {
            DocumentBuilder builder =  DocumentBuilderFactory.newInstance().newDocumentBuilder();
            DOMImplementation imple =  builder.getDOMImplementation();
            if(!imple.hasFeature("traversal", "2.0")){
                System.out.println("Traversal is required");
                return;
            }
            Document doc = null;
            try {
                doc = builder.parse("student.xml");
            } catch (SAXException ex) {
                Logger.getLogger(StudentIterator.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(StudentIterator.class.getName()).log(Level.SEVERE, null, ex);
            }
            DocumentTraversal travesal = (DocumentTraversal) doc;
            NodeIterator iterator = travesal.createNodeIterator(doc, NodeFilter.SHOW_ELEMENT, null, true);
            System.out.println("Student list");
            Node node = null;
            while ((node = iterator.nextNode()) != null) {
                System.out.println(node.getNodeName());
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(StudentIterator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void main(String[] args) {
        new StudentIterator().iterate();
    }
}
